<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 2/24/25
 * Time: 2:39 PM
 */

namespace App\Services;


use App\Models\LandingPage;
use Illuminate\Support\Str;

class LadingPageService
{


    /**
     * 落地页列表
     *
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getList($params)
    {
        $pageSize = $params['page_size'] ?? 50;
        $pageNo   = $params['page_no'] ?? '';
        $title    = $params['title'] ?? '';

        $query = LandingPage::query();

        if ($pageNo) {
            $query->where('page_no', 'like', '%' . $pageNo . '%');
        }
        if ($title) {
            $query->where('title', 'like', '%' . $title . '%');
        }
        $list = $query->orderBy('id', 'desc')->paginate($pageSize);

        foreach ($list as &$item) {
            $item->landing_page_url = env('H5_LANDING_PAGE_URL') . '?page_no=' . $item->page_no;
        }

        return $list;
    }


    /**
     * 更新落地页
     *
     * @param $params
     * @return bool
     */
    public function save($params)
    {
        $pageNo  = $params['page_no'] ?? '';
        $adminId = request()->admin->id;;
        $title          = $params['title'] ?? '';
        $headerImgUrl   = $params['header_img_url'] ?? '';
        $auditContent   = $params['audit_content'] ?? '';
        $content        = $params['content'] ?? '';
        $footerImgUrl   = $params['footer_img_url'] ?? '';
        $buttonContent  = $params['button_content'] ?? '';
        $buttonJumpLink = $params['button_jump_link'] ?? '';

        if (!$content || !$headerImgUrl) {

            return false;
        }

        if (!$pageNo) {
            $pageNo = $this->generatePageNo();
        }

        $landingPage = LandingPage::query()->firstOrNew(['page_no' => $pageNo]);

        $landingPage->admin_id         = $adminId;
        $landingPage->title            = $title;
        $landingPage->header_img_url   = $headerImgUrl;
        $landingPage->audit_content    = $auditContent;
        $landingPage->content          = $content;
        $landingPage->footer_img_url   = $footerImgUrl;
        $landingPage->button_content   = $buttonContent;
        $landingPage->button_jump_link = $buttonJumpLink;

        return $landingPage->save();
    }


    /**
     * 更改审核模式
     *
     * @param $id
     * @param int $isAudit
     * @return bool
     */
    public function updateAudit($id, $isAudit = 1)
    {
        if (!$id) {

            return false;
        }
        $landingPage = LandingPage::query()->find($id);
        if (!$landingPage) {

            return false;
        }
        if (!in_array($isAudit, [1, 2])) {

            return false;
        }

        $landingPage->is_audit = $isAudit;

        return $landingPage->save();
    }


    /**
     * 删除
     *
     * @param $id
     * @return bool|mixed|null
     */
    public function delete($id)
    {
        if (!$id) {

            return false;
        }

        $landingPage = LandingPage::query()->find($id);
        if (!$landingPage) {

            return false;
        }

        return $landingPage->delete();
    }


    private function generatePageNo()
    {
        return md5(time() . Str::random(6));
    }


    public function detail($pageNo)
    {
        $landingPage = LandingPage::query()->select(
            'page_no', 'title', 'header_img_url', 'content', 'audit_content', 'footer_img_url', 'button_content',
            'button_jump_link', 'is_audit'
        )->where('page_no', $pageNo)->first();

        if (!$landingPage) {

            return [];
        }

        $result = [
            'page_no'          => $landingPage->page_no,
            'title'            => $landingPage->title,
            'header_img_url'   => $landingPage->header_img_url,
            'footer_img_url'   => $landingPage->footer_img_url,
            'button_content'   => $landingPage->button_content,
            'button_jump_link' => $landingPage->button_jump_link,
            'content'          => ''
        ];
        if ($landingPage->is_audit == 1) {
            $result['content'] = $landingPage->audit_content;
        }
        if ($landingPage->is_audit == 2) {
            if ($landingPage->content) {
                $result['content'] = $landingPage->content;
            } else {
                $result['content'] = $landingPage->audit_content;
            }
        }

        return $result;
    }
}
